Browser Compatibility: NS2.02+, IE4+
|
Now it's time to get into some really fun stuff. Yes, variables and functions. Don't worry, it's not as bad as it sounds.....let's start with declaring variables. You'll want to keep all of your variables in the HEAD section for now. Place the declarartions between the SCRIPT tags inside the head section of your document.
To declare a variable in JavaScript, you will write something like this:
<HEAD>
<SCRIPT language="JavaScript">
<!--hide from old browsers
var name=value;
//-->
</SCRIPT>
</HEAD>
var This indicates you are about to declare a variable.
name This is the name you give the variable. Give it any name you like (other than a JavaScript reserved word such as "function" or "onMouseover".).
value This is the initial value you want the variable to have. It can be a number, words, true, false, or null.
var cars=3;
You can also use a number with a decimal. JavaScript isn't too picky about whether the value is an integer or decimal. Just type it in.
var cost=9.95;
var movie="The Lost World";
Also, if you place numbers inside the quotes, they are treated as a string rather than a numerical value.
var story=true;
var mymoney=null;
Well, nothingness isn't so great in this case, but it can be useful when you use prompts to get information from viewers, and they type in......nothing!
<HEAD>
<SCRIPT language="JavaScript">
<!--hide from old browsers
function name (parameter1, parameter2)
{
JavaScript Statements and declarations
}
//-->
</SCRIPT>
</HEAD>
function Indicates that you are going to create a function.
name This is the name you give the function. As before, name it whatever you like.
(parameter1, parameter2) The parameters are variables that are sent to the function when it is called. You can have none, one parameter, two parameters, three parameters, and so on.
{ This symbol lets you begin adding JavaScript statements and declarations.
} This indicates the end of the statements, and the end of the function.
To make use of the function, you will make a call to the function when you want to use it. You call a function by using the name, any parameters you want to send, and a semicolon, like this:
function dosomething (mymoney, cost);
So, how does one use this stuff? Well, I'll show you another way to write the "text in the status bar" script using variables and functions. Here's the trusty old link again. Move your mouse over it, and you'll see text in the status bar. Move the mouse away, and the status bar clears.
Now, here's the code that generated the link. See if you can work your way through it. I'll explain it at the end of the script.
<HEAD>
<SCRIPT language="JavaScript">
<!--hide
var text=" ";
function overlink (text)
{
window.status=text;
}
function offlink (text)
{
window.status=text;
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<A HREF="jvar.htm" onMouseover="overlink('Functions Rule!');return true"
onMouseout="offlink(' ');return true">
Place your mouse here!</A>
</BODY>
What the...? Yep, in this case the script is much longer and takes some time to work through. Here's what the script is doing:
function overlink(text) This declares a function named overlink. The function requires the variable text to be sent to it in order to work properly.
{ Begins the JavaScript Statements for the function overtext.
window.status=text; This places the value of the variable text in the status bar. The value of text was sent to the function when it was called inside the link tag, which is the string "Functions Rule!". See the explaination of the link tag for more.
} Ends the statements in the function overlink.
function offlink (text) This declares a function named offlink. The function requires the variable text to be sent to it in order to work properly.
window.status=text; This places the value of the variable text in the status bar. The value of text was sent to the function when it was called inside the link tag, which is the string " ". See the explaination of the link tag for more.
This tag calls both of the functions and passes on a string which is assigned to the variable named text. The first function, overlink, is called inside the onMouseover command. This means that when the user places their mouse over the link, the satements inside the function overlink will be executed. As you can see, overlink is called with a string variable inside the ( ) symbols. Notice we use single quotes to define this string, since double quotes are used in the onMouseover command. The string value we place here is what is sent to the function overlink, and thus is what ends up in the status bar. So, "Functions Rule!" is what shows up in the status bar when the mouse moves over the link. The onMouseout command calls the function named offlink when the mouse moves away from the link. This time, we assigned the variable text a value of blank space. The blank space is sent to the function, and is written to the status bar, erasing the "Functions Rule!" string before it. The return true phrases are there to make sure the script works by returning true.
Well, if you made it through all of that, you are ready to move on. If you still don't quite have a handle on it yet, try writing out the script on your computer, or even by hand. Sometimes it helps you work your way through the messy stuff.
Well, let's move on to some more technical stuff, in the next section: Logical Operators and Conditons.
The tutorials and articles on these pages are © 1997-99 by John Pollock and may not be reposted without written permission from the author, and may not be reprinted for profit. |
|
![]() |
Email: [email protected] |
![]() |